﻿#include <string>
#include <iostream>

using namespace std;

class Solution {
public:
	int lengthOfLastWord(string s)
	{
		if (s.empty())
		{
			return 0;
		}

		int lastNotSpacePos = -1;
		for (int i = s.length() - 1; i >= 0; --i)
		{
			if (s[i] != ' ')
			{
				lastNotSpacePos = i;
				break; 
			}
		}

		if (lastNotSpacePos == -1)
		{
			return 0;
		}

		int lastSpacePos = -1;
		for (int i = lastNotSpacePos; i >= 0; --i)
		{
			if (s[i] == ' ')
			{
				lastSpacePos = i;
				break;
			}
		}

		if (lastSpacePos == -1)
		{
			return lastNotSpacePos + 1;
		}
		return lastNotSpacePos - lastSpacePos;
	}
};

int main()
{
	Solution s;
	cout << s.lengthOfLastWord("  abb ") << endl;
	return 0;
}